/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo.dao;

/**
 *
 * @author Rodrigo
 */

import java.util.*; 
import org.hibernate.Session; 
import org.hibernate.criterion.Restrictions;

public class GenericDAO<T> {
    
    public void save(T objeto)throws DAOException
    {
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        if(!session.getTransaction().isActive())
            session.beginTransaction();
       
        try
        {
        
        session.persist(objeto);
        session.getTransaction().commit();
        
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new DAOException();
        }
        finally
        {
             
           session.close();
           
        }
        
        
    }
    
    public void update(T objeto)throws DAOException
    {
        
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        if(!session.getTransaction().isActive())
            session.beginTransaction();
       
        try
        {
        
        session.update(objeto);
        session.getTransaction().commit();
        
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new DAOException();
        }
        finally
        {
             
           session.close();
           
        }
        
    }
    
    public void delete(T objeto)throws DAOException
    {
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        
        if(!session.getTransaction().isActive())
            session.beginTransaction();
       
        try
        {
        
        session.delete(objeto);
        session.getTransaction().commit();
        
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new DAOException();
        }
        finally
        {
             
           session.close();
           
        }
        
        
    }
    
    protected T find(Integer id, Class classe)throws DAOException
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
            
        if(!session.getTransaction().isActive())
            session.beginTransaction();
       
        try
        {
        
        List<T> lista = (List<T>) session.createCriteria(classe).add(Restrictions.eq("id",id)).list();
        session.getTransaction().commit();
        if(lista.size()>0)
        return lista.get(0);
        else
            return null;
        
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new DAOException();
        }
        finally
        {
             
           session.close();
           
        }
        
    }
    
    
    protected List<T> findAll(Class classe)throws DAOException
    {      
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        
        if(!session.getTransaction().isActive())
            session.beginTransaction(); 
        
        try
        {
            
            List<T> lista = session.createCriteria(classe).list();
            session.getTransaction().commit();
            return lista; 
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new DAOException();
        }
        finally
        {
            session.close();
        }
        
    }
}
